/**
 *
 * The MIT License
 *
 * Copyright (c) 2008 Board of Trustees, Leland Stanford Jr. University. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:

 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.macdadi.ks.client;

import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.*;
import org.macdadi.ks.client.View.ViewInfo;

import java.util.ArrayList;

/**
 * The panel that contains all of the views, along with a short description
 * of each.
 */
public class ViewList extends Composite {

  private class MouseLink extends Hyperlink {

    private int index;

    public MouseLink(String name, int index) {
      super(name, name);
      this.index = index;
      sinkEvents(Event.MOUSEEVENTS);
    }

    public void onBrowserEvent(Event event) {
      switch (DOM.eventGetType(event)) {
        case Event.ONMOUSEOVER:
          mouseOver(index);
          break;

        case Event.ONMOUSEOUT:
          mouseOut(index);
          break;
      }

      super.onBrowserEvent(event);
    }
  }

  private HorizontalPanel list = new HorizontalPanel();
  private ArrayList views = new ArrayList();
    private HorizontalPanel header = new HorizontalPanel();

  private int selectedView = -1;

  public ViewList(View.Images images) {
      FlexTable table = new FlexTable();
      initWidget(table);
      table.setWidget(0, 0, images.logo().createImage());
      table.getFlexCellFormatter().setRowSpan(0, 0, 2);
      table.setWidget(0, 1, header);
      table.getFlexCellFormatter().setHorizontalAlignment(0, 1, HorizontalPanel.ALIGN_RIGHT);
      table.setWidget(1, 0, list);
      list.setStyleName("ks-List");
      table.setStyleName("ks-List");
      
//    initWidget(list);
//    list.add(images.logo().createImage());
//    setStyleName("ks-List");
  }

    public void addHeaderItem(final Widget w) {
        header.add(w);
    }

  public void addView(final ViewInfo info) {
    String name = info.getName();
    //int index = list.getWidgetCount() - 1;
    int index = list.getWidgetCount();

    MouseLink link = new MouseLink(name, index);
    list.add(link);
    views.add(info);

    list.setCellVerticalAlignment(link, HorizontalPanel.ALIGN_BOTTOM);
    styleView(index, false);
  }

  public ViewInfo find(String viewName) {
    for (int i = 0; i < views.size(); ++i) {
      ViewInfo info = (ViewInfo) views.get(i);
      if (info.getName().equals(viewName)) {
        return info;
      }
    }

    return null;
  }

  public void setViewSelection(String name) {
    if (selectedView != -1) {
      styleView(selectedView, false);
    }

    for (int i = 0; i < views.size(); ++i) {
      ViewInfo info = (ViewInfo) views.get(i);
      if (info.getName().equals(name)) {
        selectedView = i;
        styleView(selectedView, true);
        return;
      }
    }
  }

  private void colorView(int index, boolean on) {
    String color = "";
    if (on) {
      color = ((ViewInfo) views.get(index)).getColor();
    }

    //Widget w = list.getWidget(index + 1);
    Widget w = list.getWidget(index);
    DOM.setStyleAttribute(w.getElement(), "backgroundColor", color);
  }

  private void mouseOut(int index) {
    if (index != selectedView) {
      colorView(index, false);
    }
  }

  private void mouseOver(int index) {
    if (index != selectedView) {
      colorView(index, true);
    }
  }

  private void styleView(int index, boolean selected) {
    String style = (index == 0) ? "ks-FirstViewItem" : "ks-ViewItem";
    if (selected) {
      style += "-selected";
    }

    //Widget w = list.getWidget(index + 1);
    Widget w = list.getWidget(index);
    w.setStyleName(style);

    colorView(index, selected);
  }
}
